package com.dreamstations.changshads.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import com.dreamstations.changshads.R;
import com.dreamstations.changshads.entity.BookDetailBean;
import com.dreamstations.changshads.entity.DatumBean;
import com.dreamstations.changshads.entity.FigureBean;
import com.dreamstations.changshads.entity.InvestigateBean;
import com.dreamstations.changshads.entity.LibraryBean;
import com.dreamstations.changshads.entity.ResourceBean;
import com.dreamstations.changshads.entity.StoryBean;
import com.dreamstations.changshads.entity.TopicBean;
import com.dreamstations.changshads.entity.WorkBean;
import com.dreamstations.changshads.util.DSRequest;
import com.dreamstations.changshads.util.DSUtils;
import com.dreamstations.changshads.util.RequestType;

import java.util.List;

public class FactoryActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView tv1, tv2;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initViewDev();

    }

    private void initViewDev() {
        setContentView(R.layout.activity_factory);
        tv1 = (TextView) findViewById(R.id.tv1);
        tv2 = (TextView) findViewById(R.id.tv2);
    }

    @Override
    public void onClick(View v) {
        tv1.setText("正在发起请求。。。");
        switch (v.getId()) {
            case R.id.btn1:
                getWorkBean();
                break;
            case R.id.button2:
                getDatumBean();
                break;
            case R.id.button3:
                getFigureBean();
                break;
            case R.id.button4:
                getTopicBean();
                break;
            case R.id.button5:
                getInvestigateBean();
                break;
            case R.id.button6:
                getLibraryBean();
                break;
            case R.id.button8:
                getResourceBean();
                break;
            case R.id.button9:
                getStoryBean();
                break;
            case R.id.bookdetail:
                getBookDetail();
                break;
            case R.id.btn_home: {
                startActivity(new Intent(getApplicationContext(), HomeActivity.class));
                finish();
            }
            break;
            case R.id.act_section: {
                startActivity(new Intent(getApplicationContext(), SectionActivity.class));
            }
            break;
            case R.id.act_actcledetail: {
//                new DSRequest()
//                        .type(RequestType.TYPE_FIGURE)
//                        .addParams("typeId","1")
//                        .addParams("page","1")
//                        .addParams("front","1")
//                        .cacheType(DSUtils.ONLY_NETWORK)
//                        .build()
//                        .execute(new DSRequest.DSCallback<List<FigureBean>>() {
//                            @Override
//                            public void onResponse(List<FigureBean> result) {
                FigureBean data = new FigureBean();
                data.setTitle("testsetetaest");
                data.setContent("[{\"url\":\"/NewParty/image/other/work/201509230944137453.jpg\",\"doc\":\"111\"}]");
                Intent intent = new Intent(getApplicationContext(), ArticleDetailActivity.class);
                intent.putExtra("data", data);
                startActivity(intent);
//                            }
//
//                            @Override
//                            public void onFailure(String msg) {
//
//                            }
//                        });
            }
            break;
            case R.id.act_pictopic: {
                startActivity(new Intent(getApplicationContext(), PicTopicActivity.class));
            }
            break;
            case R.id.act_arcticleweb: {
                new DSRequest()
                        .type(RequestType.TYPE_FIGURE)
                        .addParams("typeId", "1")
                        .addParams("page", "1")
                        .cacheType(DSUtils.ONLY_NETWORK)
                        .build()
                        .execute(new DSRequest.DSCallback<List<FigureBean>>() {
                            @Override
                            public void onResponse(List<FigureBean> result) {
                                Intent intent = new Intent(getApplicationContext(), ArticleDetail_WebActivity.class);
                                intent.putExtra("data", result.get(0));
                                startActivity(intent);
                            }

                            @Override
                            public void onFailure(String msg) {

                            }
                        });
            }
            break;
        }
    }

    private void getBookDetail() {
        new DSRequest()
                .type(RequestType.TYPE_BOOKDETAIL)
                .addParams("page", String.valueOf(1))
                .addParams("bookId", String.valueOf(1))
                .cacheType(DSUtils.ONLY_NETWORK)
                .build()
                .execute(new DSRequest.DSCallback<List<BookDetailBean>>() {
                    @Override
                    public void onResponse(List<BookDetailBean> result) {
                        tv1.setText(result.get(0).toString());
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                });
    }

    private void getDatumBean() {
        new DSRequest()
                .type(RequestType.TYPE_DATUM)
                .addParams("page", String.valueOf(1))
                .addParams("typeId", String.valueOf(5))
                .addParams("front", "1")
                .cacheType(DSUtils.ONLY_NETWORK)
                .build()
                .execute(new DSRequest.DSCallback<List<DatumBean>>() {
                    @Override
                    public void onResponse(List<DatumBean> result) {
                        tv1.setText(result.get(0).toString());
                    }

                    @Override
                    public void onFailure(String msg) {
                        tv1.setText(msg);
                    }
                });

    }

    private void getTopicBean() {
        new DSRequest()
                .type(RequestType.TYPE_TOPIC)
                .addParams("page", String.valueOf(1))
                .addParams("typeId", String.valueOf(1))
                .addParams("front", "1")
                .cacheType(DSUtils.ONLY_NETWORK)
                .build()
                .execute(new DSRequest.DSCallback<List<TopicBean>>() {
                    @Override
                    public void onResponse(List<TopicBean> result) {
                        tv1.setText(result.get(0).toString());
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                });
    }

    private void getFigureBean() {
        new DSRequest()
                .type(RequestType.TYPE_FIGURE)
                .addParams("page", "1")
                .addParams("typeId", String.valueOf(1))
                .addParams("front", "1")
                .cacheType(DSUtils.ONLY_NETWORK)
                .build()
                .execute(new DSRequest.DSCallback<List<FigureBean>>() {
                    @Override
                    public void onResponse(List<FigureBean> result) {
                        if (result == null) {
                            onFailure("结果为空");
                            return;
                        }
                        tv1.setText(result.get(0).toString());
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                });
    }

    private void getInvestigateBean() {
        new DSRequest()
                .type(RequestType.TYPE_INVESTIGATE)
                .addParams("page", String.valueOf(1))
                .addParams("typeId", String.valueOf(1))
                .addParams("front", "1")
                .cacheType(DSUtils.ONLY_NETWORK)
                .build()
                .execute(new DSRequest.DSCallback<List<InvestigateBean>>() {
                    @Override
                    public void onResponse(List<InvestigateBean> result) {
                        if (result.size() == 0) {
                            onFailure("结果为空");
                            return;
                        }
                        tv1.setText(result.get(0).toString());
                    }

                    @Override
                    public void onFailure(String msg) {
                        tv1.setText(msg);
                    }
                });
    }

    private void getLibraryBean() {
        new DSRequest()
                .type(RequestType.TYPE_LIBRARY)
                .addParams("page", String.valueOf(1))
                .addParams("typeId", String.valueOf(1))
                .addParams("front", "1")
                .cacheType(DSUtils.ONLY_NETWORK)
                .build()
                .execute(new DSRequest.DSCallback<List<LibraryBean>>() {
                    @Override
                    public void onResponse(List<LibraryBean> result) {
                        tv1.setText(result.get(0).toString());
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                });
    }

    private void getResourceBean() {
        new DSRequest()
                .type(RequestType.TYPE_RESOURCE)
                .addParams("page", String.valueOf(1))
                .addParams("typeId", String.valueOf(1))
                .addParams("front", "1")
                .cacheType(DSUtils.ONLY_NETWORK)
                .build()
                .execute(new DSRequest.DSCallback<List<ResourceBean>>() {
                    @Override
                    public void onResponse(List<ResourceBean> result) {
                        tv1.setText(result.get(0).toString());
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                });
    }

    private void getStoryBean() {
        new DSRequest()
                .type(RequestType.TYPE_STORY)
                .addParams("page", String.valueOf(1))
                .addParams("typeId", String.valueOf(1))
                .addParams("front", "1")
                .cacheType(DSUtils.ONLY_NETWORK)
                .build()
                .execute(new DSRequest.DSCallback<List<StoryBean>>() {
                    @Override
                    public void onResponse(List<StoryBean> result) {
                        tv1.setText(result.get(0).toString());
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                });
    }

    private void getWorkBean() {
//        tv2.setText(DSOfflineDataProvider.JSON_WORK);
        new DSRequest()
                .type(RequestType.TYPE_WORK)    //请求的数据是什么类型
                .addParams("typeId", String.valueOf(1))    //添加参数，需要什么参数自己先加进去，然后做好记录，到时候汇总给后台，可以不设
                .addParams("page", String.valueOf(1))    //添加参数，需要什么参数自己先加进去，然后做好记录，到时候汇总给后台，可以不设
                .addParams("front", "1").cacheType(DSUtils.ONLY_NETWORK)         //缓存读取策略，默认是NETWORK_ELSE_CACHE，可以不设
                .build()
                .execute(new DSRequest.DSCallback<List<WorkBean>>() {
                    //                                       这里的WorkBean是指定onResponse方法中的参数类型，目的是为了省去onResponse方法中的强制转换步骤
                    //相当于在内部事先做过一次强制转换了
                    @Override
                    public void onResponse(List<WorkBean> result) {
                        tv1.setText(result.get(0).toString());

                    }

                    @Override
                    public void onFailure(String msg) {
                        tv1.setText(msg);
                    }
                });
    }
}
